package com.whfc.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.ConfirmationRecordMapper;
import com.whfc.emp.entity.ConfirmationRecord;
import com.whfc.emp.service.ClassMeetingSignService;
import com.whfc.emp.service.ConfirmationRecordService;
import com.whfc.entity.dto.Apply.rep.ArrivalConfirmationDTO;
import com.whfc.entity.dto.Apply.req.AppEntryApplicationReq;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@DubboService(interfaceClass = ConfirmationRecordService.class, version = "1.0.0",timeout = 30000)
public class ConfirmationRecordServiceImpl implements ConfirmationRecordService {

    @Resource
    private ConfirmationRecordMapper confirmationRecordMapper;

    @Override
    public int addRecord(ConfirmationRecord record) {
        return confirmationRecordMapper.insert(record);
    }

    @Override
    public int addRecordSelective(ConfirmationRecord record) {
        return confirmationRecordMapper.insertSelective(record);
    }

    @Override
    public ConfirmationRecord getById(Integer id) {
        return confirmationRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public ConfirmationRecord getByPlanId(Integer planId) {
        return confirmationRecordMapper.selectByPlanId(planId);
    }

    @Override
    public List<ConfirmationRecord> getByConfirmer(Integer confirmerId) {
        return confirmationRecordMapper.selectByConfirmer(confirmerId);
    }

    @Override
    public PageData<ConfirmationRecord> getAllByPage(Integer planId, int pageNum, int pageSize) {
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询所有记录，PageHelper会自动进行分页
        List<ConfirmationRecord> list = confirmationRecordMapper.selectAll(planId);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public List<ConfirmationRecord> getByCondition(ConfirmationRecord record) {
        return confirmationRecordMapper.selectByCondition(record);
    }

    @Transactional
    @Override
    public int confirmArrival(ConfirmationRecord record) {
        // 检查是否已存在该计划的记录
        ConfirmationRecord existRecord = getByPlanId(record.getPlanId());
        if (existRecord == null) {
            // 不存在则创建新记录
            record.setArrivalConfirmTime(null); // 由数据库自动填充当前时间
            return addRecordSelective(record);
        } else {
            // 存在则更新到场信息
            existRecord.setArrivalStaff(record.getArrivalStaff());
            existRecord.setArrivalDevice(record.getArrivalDevice());
            existRecord.setArrivalConfirmer(record.getArrivalConfirmer());
            existRecord.setArrivalConfirmerName(record.getArrivalConfirmerName());
            return confirmationRecordMapper.updateArrivalInfo(existRecord);
        }
    }

    @Transactional
    @Override
    public int confirmDeparture(ConfirmationRecord record) {
        // 检查是否已存在该计划的记录
        ConfirmationRecord existRecord = getByPlanId(record.getPlanId());
        if (existRecord == null) {
            throw new RuntimeException("未找到对应的到场记录，无法进行离场确认");
        }
        // 更新离场信息
        existRecord.setDepartureStaff(record.getDepartureStaff());
        existRecord.setDepartureDevice(record.getDepartureDevice());
        existRecord.setDepartureConfirmer(record.getDepartureConfirmer());
        existRecord.setDepartureConfirmerName(record.getDepartureConfirmerName());
        return confirmationRecordMapper.updateDepartureInfo(existRecord);
    }

    @Override
    public int updateRecord(ConfirmationRecord record) {
        return confirmationRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateRecordSelective(ConfirmationRecord record) {
        return confirmationRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteById(Integer id) {
        return confirmationRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByPlanId(Integer planId) {
        return confirmationRecordMapper.deleteByPlanId(planId);
    }

    @Transactional
    @Override
    public int batchDelete(List<Integer> ids) {
        int count = 0;
        for (Integer id : ids) {
            count += deleteById(id);
        }
        return count;
    }

    @Override
    public List<ArrivalConfirmationDTO> selectArrivalConfirmationList(AppEntryApplicationReq req) {
        return confirmationRecordMapper.selectArrivalConfirmationList(req);
    }

    @Override
    public List<ArrivalConfirmationDTO> selectArrivalConfirmationExitList(AppEntryApplicationReq req) {
        return confirmationRecordMapper.selectArrivalConfirmationExitList(req);
    }
}